package com.cays.myresearch.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.cays.myresearch.R;
import com.cays.myresearch.Util.HttpUtil;
import com.cays.myresearch.action.LoginAction;
import com.cays.myresearch.constant.Constant;
import com.cays.myresearch.entity.ResponseInfo;
import com.cays.myresearch.entity.Student;
import com.cays.myresearch.entity.UserInfo;
import com.cays.myresearch.ui.ImageText;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView loginBack;
    private EditText loginUserName,loginUserPassword;
    private CheckBox loginCheckAutologin;
    private RadioGroup radioGroup;
    private Button loginBtn,registerBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        initView();
    }
    private void initView(){
        loginBack=(ImageView)findViewById(R.id.login_back);
        loginBack.setOnClickListener(this);
        loginUserName=(EditText)findViewById(R.id.login_user_name);
        loginUserPassword=(EditText)findViewById(R.id.login_user_password);
        loginCheckAutologin=(CheckBox)findViewById(R.id.login_check_autologin);
        loginBtn=(Button)findViewById(R.id.login_login_btn);
        loginBtn.setOnClickListener(this);
        registerBtn=(Button)findViewById(R.id.login_register_btn);
        registerBtn.setOnClickListener(this);
        radioGroup=(RadioGroup)findViewById(R.id.login_check_type);
        LoginAction loginAction=new LoginAction();
        if (loginAction.hasUserInfo(getApplicationContext())){
            UserInfo userInfo=loginAction.getUserInfo(getApplicationContext());
            loginUserName.setText(userInfo.getName());
            loginUserPassword.setText(userInfo.getPassword());
            loginCheckAutologin.setChecked(true);
            if(userInfo.getType()==Constant.user_student) radioGroup.check(R.id.login_check_student);
            if(userInfo.getType()==Constant.user_teacher) radioGroup.check(R.id.login_check_teacher);
            if(userInfo.getType()==Constant.user_staff) radioGroup.check(R.id.login_check_staff);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_back:finish();break;
            case R.id.login_login_btn:
                loginMyResearch();
                break;
            case R.id.login_register_btn:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
            default:break;
        }
    }
    Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0){
                Toast.makeText(getApplicationContext(),"未知错误",
                        Toast.LENGTH_LONG).show();
            }else if(msg.what==1){
                String s=(String)msg.obj;
                if(s.contains("~")){
                    String []ss=s.split("~");
                    ResponseInfo info=Constant.gson.fromJson(ss[0],
                            new TypeToken<ResponseInfo>(){}.getType());
                    UserInfo userInfo1=Constant.gson.fromJson(ss[1],
                            new TypeToken<UserInfo>(){}.getType());
                    if (info.getResponseCode()==200) {
                        new LoginAction().saveUserInfo(getApplicationContext(), userInfo1);
                        finish();
                    }else{
                        Toast.makeText(getApplicationContext(),"用户名或密码错误",
                                Toast.LENGTH_LONG).show();
                    }
                }else {
                    Toast.makeText(getApplicationContext(),"用户名或密码错误",
                            Toast.LENGTH_LONG).show();
                }
            }else if(msg.what==2){
                Toast.makeText(getApplicationContext(),"用户名或密码错误",
                        Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    public void loginMyResearch(){
        String name=loginUserName.getText().toString();
        String password=loginUserPassword.getText().toString();
        boolean isAutoLogin=loginCheckAutologin.isChecked();
        int type=0,radioId=radioGroup.getCheckedRadioButtonId();
        if(radioId==R.id.login_check_student)type= Constant.user_student;
        else if(radioId==R.id.login_check_teacher)type= Constant.user_teacher;
        else if(radioId==R.id.login_check_staff)type= Constant.user_staff;
        final String url=Constant.URL+"login/login";
        final UserInfo userInfo=new UserInfo(name,password,isAutoLogin,type,"");
        final MultipartBody.Builder builder=new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("userInfo",Constant.gson.toJson(userInfo));
        HttpUtil.sendHttpRequest(url,builder,handler,1,0);
    }
}
